package simulation;
import javax.swing.JFrame;

import events.Event;
import events.LeaveTeleSkiD;
import events.WelcomeEvent;


public class Simulation {

	
	protected static double[] moy = new double[3];
	private static int count;
	
	public static void main(String[] args) {
		final View view = new View();
		final Echeancier e = new Echeancier();
		Event evt = new WelcomeEvent();
		evt.date = Echeancier.T9H;
		e.ajouter(evt);
		
		Thread thread = new Thread(){
			@Override
			public void run() {
				//super.run();
				while(e.isNotEmpty()){
				
					Event ev = e.retourneEtEnlevePremier();
					ev.treat(e);
					updateMoy();
					view.time.setText(Echeancier.toHour(ev.date));
					
					//System.out.print(e.afficheLaSituation());
					//StationState.display();
					refresh(view);
					
					try {
						sleep(0);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
				view.time.setText(getInfos());
			}
		};
		
		JFrame frame = new JFrame(" Station de ski ");		
		frame.setContentPane(view);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.pack();
		
		thread.start();
		
		
	}
	
	public static void refresh(View view){
		view.B.setValue(StationState.B);
		view.C.setValue(StationState.C);
		view.D.setValue(StationState.D);
		
		view.waitB.setValue(StationState.WB);
		view.waitC.setValue(StationState.WC);
		view.waitD.setValue(StationState.WD);
		
		view.P1.setValue(StationState.p1);
		view.P2A.setValue(StationState.p2A);
		view.P2B.setValue(StationState.p2B);
		view.P3.setValue(StationState.p3);
		view.P4.setValue(StationState.p4);
	}
	
	private static void updateMoy() {
		count++;
		moy[0] += StationState.WB;
		moy[1] += StationState.WC;
		moy[2] += StationState.WD;
	}
	
	private static String getInfos(){
		return "\tMoyenne des attentes : \n\t\tB="+(moy[0]/count)+" C="+(moy[1]/count)+" D="+(moy[2]/count)
		+"\n\tNombre moyen de passages de chaque skieur par le teleski D : "+passageD(); 
	}
	
	private static double passageD(){
		double c = LeaveTeleSkiD.NB_PASSAGE_IN_D;
		return c/WelcomeEvent.NB_SKIEUR_TOTAL;
	}

}
